package com.tssup.fesco.util;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.xxl.job.core.log.XxlJobLogger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

/**
 *
 * @author jiashen
 * @date 17-6-10
 */
@Service
public class TaskLogger {

    private Logger logger =  LoggerFactory.getLogger(this.getClass());

    public void info(String msg, Exception e) {
        logger.info(msg, e);
        XxlJobLogger.log(getErrorInfoFromException(e));
    }

    public void info(String msg) {
        logger.info(msg);
        XxlJobLogger.log(msg);
    }

    public void debug(String msg, Exception e) {
        logger.info(msg, e);
        XxlJobLogger.log(getErrorInfoFromException(e));
    }

    public void debug(String msg) {
        logger.info(msg);
        XxlJobLogger.log(msg);
    }

    public void error(String msg, Exception e) {
        logger.info(msg, e);
        XxlJobLogger.log(getErrorInfoFromException(e));
    }

    public void error(String msg) {
        logger.info(msg);
        XxlJobLogger.log(msg);
    }

    private static String getErrorInfoFromException(Exception e) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            e.printStackTrace(pw);
            return "\r\n" + sw.toString() + "\r\n";
        } catch (Exception e2) {
            return "GetErrorInfoFromException get error: " + e2.toString();
        }
    }
}
